package br.rui.cartoes.visao.servlet;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.rui.cartoes.dominio.Extrato;
import br.rui.cartoes.negocio.Fachada;


/**
 * Servlet Class
 *
 * @web.servlet              name="Acao"
 *                           display-name="Name for Acao"
 *                           description="Description for Acao"
 * @web.servlet-mapping      url-pattern="/Acao"
 * @web.servlet-init-param   name="A parameter"
 *                           value="A value"
 */
@SuppressWarnings("serial")
public class PaginaServlet extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException,
		IOException {
		doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException,
		IOException {
		String acao = req.getParameter("acao");
		String url = "";
		if(acao!=null){
			try {			
				if(acao.equalsIgnoreCase("cadastraExtrato")){
					setSessao(req);
					url = "web/index.jsp?acao=" + acao;
				}
				else if(acao.equalsIgnoreCase("cadastraLancamentos")){
					url = "web/index.jsp?acao=" + acao;
				}
				else if(acao.equalsIgnoreCase("consultaExtratos")){
					setSessao(req);
					url = "web/index.jsp?acao=" + acao;
				}
				else if(acao.equalsIgnoreCase("consultaConfronto")){
					setSessaoCliente(req);
					url = "web/index.jsp?acao=" + acao;
				}
				else if(acao.equalsIgnoreCase("cadastraLancamento")){
					setRequestExtrato(req);
					url = "web/index.jsp?acao=" + acao;
				}
				else if(acao.equalsIgnoreCase("exportacaoAVista")){
					setSessaoCliente(req);
					url = "web/index.jsp?acao=" + acao;
				}
				else if(acao.equalsIgnoreCase("importarFaturamento")){
					setSessaoCliente(req);
					url = "web/index.jsp?acao=" + acao;
				}
				else if(acao.equalsIgnoreCase("alteraCliente")){
					setSessao(req);
					url = "web/index.jsp?acao=" + acao;
				}
				else if(acao.equalsIgnoreCase("cadastraCliente")){
					setSessao(req);
					url = "web/index.jsp?acao=" + acao;
				}
				else if(acao.equalsIgnoreCase("cadastraOperadora")){
					url = "web/index.jsp?acao=" + acao;
				}
				else if(acao.equalsIgnoreCase("home")){
					url = "web/index.jsp?acao=" + acao;
				}	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		req.getRequestDispatcher(url).forward(req,resp);
	}
	
	private void setRequestExtrato(HttpServletRequest req) {
		Extrato extrato = Fachada.findExtratoByCode(Integer.parseInt(req.getParameter("extrato")));
		req.setAttribute("extrato",extrato);
		
	}

	private void setSessao (HttpServletRequest req){
		removeTaxas(req);
		setSessaoCliente(req);
		setSessaoOperadora(req);
		setSessaoTipoTaxa(req);
	}
	
	private void setSessaoCliente(HttpServletRequest req){
		HttpSession session = req.getSession(true);
		Collection clientes = Fachada.obterClientes();
		session.setAttribute("clientes",clientes);
	}
	
	private void setSessaoOperadora(HttpServletRequest req){
		HttpSession session = req.getSession();
		Collection operadoras = Fachada.obterOperadoras();
		session.setAttribute("operadoras",operadoras);
	}
	
	private void setSessaoTipoTaxa(HttpServletRequest req){
		HttpSession session = req.getSession();
		Collection tiposTaxa = Fachada.obterTiposTaxa();
		session.setAttribute("tiposTaxa",tiposTaxa);
	}
	
	private void removeTaxas(HttpServletRequest req){
		HttpSession session = req.getSession();
		session.removeAttribute("taxas");
	}
}
